//
//  SXTRegisterView.m
//  SYT
//
//  Created by 彭之耀 on 2017/9/4.
//  Copyright © 2017年 个人学习. All rights reserved.
//

#import "SXTRegisterView.h"

@interface SXTRegisterView ()<UITextFieldDelegate>

@property (strong,nonatomic) UILabel *tostLabel;           //提示Label
@property (strong,nonatomic) UITextField *userNameText;    //手机号码
@property (strong,nonatomic) UITextField *passwordText;    //密码
@property (strong,nonatomic) UIButton *nextButton;         //下一步
@property (strong,nonatomic) UIButton *goLoginButton;      //去登陆

/**
 textField的背景，用来设置边框
 */
@property (strong,nonatomic) UILabel *textBackLabel;
/**
 用户名、密码之间的分割线
 */
@property (strong,nonatomic) UILabel *centerLine;

@end

@implementation SXTRegisterView
static BOOL UserNameIsOk = NO;     //用于判断userName是否满足条件
static BOOL passWordIsOk = NO;

/*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect {
 // Drawing code
 }
 */

-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self addSubview:self.tostLabel];
        [self addSubview:self.textBackLabel];
        [self addSubview:self.userNameText];
        [self addSubview:self.passwordText];
        [self addSubview:self.centerLine];
        [self addSubview:self.nextButton];
        [self addSubview:self.goLoginButton];
        
    }
    return self;
}


/**
 这个方法是userName创建的方法，每当输入的文字变动时，都能够实时监听到，而使用代理总是不行！可能是因为代理方法比文字的改变发生的时间更早，以至于无法获得最新的文字变动情况。
 此方法发生在“UIControlEventEditingChanged”--即文字发生改变时调用。

 @param textField 调用该方法的textField；
 */
-(void)userNameTextChange:(UITextField *)textField{
    if (textField.text.length >= 11) {
        UserNameIsOk = YES;
    }else{
        UserNameIsOk = NO;
    }
    [self changeNextButton];
}

-(void)passwordTextChange:(UITextField *)textField{
    if (textField.text.length > 5) {
        passWordIsOk = YES;
    }else{
        passWordIsOk = NO;
    }
    [self changeNextButton];
}

/**
 此方法用于改变“下一步”按钮的状态，包括：显示状态及是否能够点击。
 */
-(void)changeNextButton{
    if (passWordIsOk && UserNameIsOk) {
        self.nextButton.backgroundColor = rgb(56, 166, 241);
        self.nextButton.userInteractionEnabled = YES;
    }else{
        self.nextButton.backgroundColor = rgb(229, 229, 229);
        self.nextButton.userInteractionEnabled = NO;
    }
}

/**
 "下一步"按钮的点击事件
 */
-(void)onClickNextButton{
    //如果存在这个block，则执行;
    if (_pushNextBlock) {
        self.pushNextBlock(@{@"userName":_userNameText.text,@"password":_passwordText.text});
    }
}

#pragma 控件初始化
-(void)layoutSubviews{
    [super layoutSubviews];
    __weak typeof (self)weakSelf = self;
    [_tostLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(@35);
        make.top.right.equalTo(weakSelf);
        make.left.equalTo(weakSelf.mas_left).offset(15);
    }];
    [_userNameText mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf).offset(15);
        make.right.equalTo(weakSelf);
        make.height.equalTo(@44);
        make.top.equalTo(weakSelf.tostLabel.mas_bottom);
    }];
    [_passwordText mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf).offset(15);
        make.right.equalTo(weakSelf);
        make.height.equalTo(@44);
        make.top.equalTo(weakSelf.userNameText.mas_bottom);
    }];
    
    [_nextButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.mas_left).offset(16);
        make.right.equalTo(weakSelf.mas_right).offset(-16);
        make.height.equalTo(@35);
        make.top.equalTo(weakSelf.passwordText.mas_bottom).offset(15);
    }];
    [_goLoginButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(70, 16));
        make.right.equalTo(weakSelf.mas_right).offset(-15);
        make.top.equalTo(weakSelf.nextButton.mas_bottom).offset(23);
    }];
    
    [_textBackLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.tostLabel.mas_bottom).offset(-1);
        make.height.mas_equalTo(@90);
        make.centerX.equalTo(weakSelf.mas_centerX);
        make.width.mas_equalTo(VIEW_WIDTH+2);
    }];
    
    [_centerLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(weakSelf.textBackLabel.mas_centerY);
        make.height.mas_equalTo(@1);
        make.left.equalTo(weakSelf.mas_left).offset(15);
        make.right.equalTo(weakSelf.mas_right).offset(-15);
    }];
}

-(UIButton *)goLoginButton{
    if (!_goLoginButton) {
        _goLoginButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_goLoginButton setTitle:@"去登陆" forState:UIControlStateNormal];
        [_goLoginButton setTitleColor:rgb(56, 145, 241) forState:UIControlStateNormal];
        _goLoginButton.backgroundColor = MainColor;
    }
    return _goLoginButton;
}

-(UILabel *)tostLabel{
    if (!_tostLabel) {
        _tostLabel = [[UILabel alloc]init];
        _tostLabel.font = [UIFont systemFontOfSize:14.0];
        _tostLabel.text = @"请输入手机号码注册新用户";
        _tostLabel.textColor = rgb(81, 81, 81);
    }
    return _tostLabel;
}

-(UITextField *)userNameText{
    if (!_userNameText) {
        _userNameText = [[UITextField alloc]init];
        _userNameText.delegate = self;
        _userNameText.placeholder = @"请输入手机号";
        _userNameText.backgroundColor = [UIColor whiteColor];
        _userNameText.keyboardType = UIKeyboardTypeNumberPad;  //数字键盘
        [_userNameText addTarget:self action:@selector(userNameTextChange:) forControlEvents:UIControlEventEditingChanged];
    }
    return _userNameText;
}

-(UITextField *)passwordText{
    if (!_passwordText) {
        _passwordText = [[UITextField alloc]init];
        _passwordText.delegate = self;
        _passwordText.placeholder = @"设置账号密码";
        _passwordText.backgroundColor = [UIColor whiteColor];
        _passwordText.secureTextEntry = YES; //设置输入文字不可见，即为密码输入
        [_passwordText addTarget:self action:@selector(passwordTextChange:) forControlEvents:UIControlEventEditingChanged];
    }
    return _passwordText;
}

-(UIButton *)nextButton{
    if (!_nextButton) {
        _nextButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_nextButton setTitle:@"下一步" forState:UIControlStateNormal];
        _nextButton.backgroundColor = rgb(229, 229, 229);
        [_nextButton addTarget:self action:@selector(onClickNextButton) forControlEvents:UIControlEventTouchUpInside];
        _nextButton.userInteractionEnabled = NO;
        [_nextButton setTitleColor:rgb(132, 132, 132) forState:UIControlStateNormal];
    }
    return _nextButton;
}

-(UILabel *)textBackLabel{
    if (!_textBackLabel) {
        _textBackLabel = [[UILabel alloc]init];
        _textBackLabel.backgroundColor = [UIColor whiteColor];
        _textBackLabel.layer.borderWidth = 1;
        _textBackLabel.layer.borderColor = rgb( 188, 188, 188).CGColor;
    }
    return _textBackLabel;
}

-(UILabel *)centerLine{
    if (!_centerLine) {
        _centerLine = [[UILabel alloc]init];
        _centerLine.backgroundColor = rgb(188, 188, 188);
    }
    return _centerLine;
}

#pragma textFieldDelegate
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    //string未键入值
    if (textField == self.userNameText) {
        
        //如果“键入值”在“0123456789”中，且输入框文字长度未达到11位时，可以键入
        if ([@"0123456789" containsString:string] && textField.text.length<=10) {
            return YES;
        }
        
        //如果输入的是“删除键”，也是可以键入的。
        if (string.length == 0) {
            return YES;
        }
        return NO;
    }
    return YES;
}



@end
